import { message } from "antd";
import { useEffect, useRef, useState } from "react";
import { $eventBus, $EventBus_off, $EventBus_on, $export_excel } from "../../../../../../common/utils";
import { $Http } from "../../../../../../network";
import HomePage_TableComponent from "../../../../component/table";
import HomePage_ModalComponent from "../../../../component/modal";
import HomePage_DataMaintenancePage_SubstationMaintenancePage_SubstationModalComponent from "../../../dataMaintenance/page/substationMaintenance/component/substationModal";

// 主页-统计查询页面-站点台账页面
const HomePage_StatisticalQueryPage_SubstationLedgerPage: React.FC = () => {


  const paramsData = useRef<object>({})
  const initParamsData = useRef<object>({})
  let [pageNo, setPageNo] = useState<number>(1)
  let [pageSize, setPageSize] = useState<number>(10)
  const [sourceData, setSourceData] = useState<object>({})
  const [isModal, setIsModal] = useState<boolean>(false)
  const [isDeleteModal, setIsDeleteModal] = useState<boolean>(false)
  const [modalData, setModalData] = useState<object>({})

  const columnList = [
    {
      title: '序号',
      width: 60,
      render: (text: string, record: object, idx: number) => idx + 1,
    },
    {
      title: '巡维中心',
      minWidth: 85,
      dataIndex: 'maintainCentre',
      render: (text: string) => text ?? "-"
    },
    {
      title: '站点名称',
      minWidth: 85,
      dataIndex: 'stationName',
      render: (text: string) => text ?? "-"
    },

    {
      title: '电压等级',
      minWidth: 85,
      dataIndex: 'volLevel',
      render: (text: string) => text ?? "-"
    },
    {
      title: '主变台数',
      minWidth: 85,
      dataIndex: 'mainCount',
      render: (text: string) => text ?? "-"
    },
    {
      title: '监测对象',
      minWidth: 85,
      dataIndex: 'monitorMain',
      render: (text: Array<string>, record: object, idx: number) => text ? text.join("、") : "-",
    },
    {
      title: '接地点',
      minWidth: 70,
      dataIndex: 'groundPoint',
      render: (text: Array<string>, record: object, idx: number) => text ? text.join("、") : "-",
    },
    {
      title: '经度',
      minWidth: 60,
      dataIndex: 'longitude',
      render: (text: string) => text ?? "-"
    },
    {
      title: '维度',
      minWidth: 60,
      dataIndex: 'latitude',
      render: (text: string) => text ?? "-"
    },
    {
      title: '值班电话',
      minWidth: 85,
      dataIndex: 'tel',
      render: (text: string) => text ?? "-"
    },
    {
      title: '站点地址',
      minWidth: 85,
      dataIndex: 'address',
      render: (text: string) => text ?? "-"
    },
    {
      title: '操作',
      align: "right",
      width: 60,
      fixed: "right",
      render: (text: string, record: object, idx: number) => <span className="homePage-tableOperationBtn" onClick={() => click_operation(record)}>查看</span>
    },

  ];
  const click_operation = (data: object) => {
    setIsModal(true)
    setModalData({ type: "see", ...data })
  }
  const http_search = () => {
    $Http("search", { typeName: "数据维护/站点维护", ...paramsData.current, pageNo, pageSize }).then((res) => {
      setSourceData({ ...res["data"] })
    })
  }

  const change_pagination = (no: number, size: number) => {
    pageNo = no
    pageSize = size
    setPageNo(pageNo)
    setPageSize(pageSize)
    http_search()
  }

  const change_export = (list: Array<number>) => {
    $export_excel("统计查询/站点台账", list)
  }

  // 广播函数数据
  const eventBusFuncData = {
    // 头部操作初始参数
    headerOperationInitParamsData: (data: object) => {
      initParamsData.current = data["data"]["initParamsData"]
      paramsData.current = data["data"]["initParamsData"]
      http_search()
    },
    // 头部操作查询
    headerOperationSearch: (data: object) => {
      pageNo = 1
      setPageNo(pageNo)
      paramsData.current = { ...paramsData.current, ...data["data"]["paramsData"] }
      http_search()
    },
    // 头部操作条件的开关
    headerOperationConditionSwitch: (data: object) => {
      pageNo = 1
      setPageNo(pageNo)
      paramsData.current = { ...initParamsData.current }
      http_search()
    },
  }


  useEffect(() => {
    $EventBus_on(eventBusFuncData)
    return () => {
      $EventBus_off(eventBusFuncData)
    }
  }, [])
  return (
    <>
      <HomePage_TableComponent name="homePageStatisticalQueryPageSubstationLedgerPageTable" columnList={columnList} isRowSelection highlightList={["站点名称"]} alignRightList={["操作"]} pageNo={pageNo} pageSize={pageSize} sourceData={sourceData} change_pagination={change_pagination} change_selectedRowList={change_export} />
      {
        isModal && <HomePage_ModalComponent width={640} change_cancel={setIsModal} contentComponent={<HomePage_DataMaintenancePage_SubstationMaintenancePage_SubstationModalComponent sourceData={modalData} change_cancel={setIsModal} />} />
      }
    </>
  )
}

export default HomePage_StatisticalQueryPage_SubstationLedgerPage